package com.julian.apps.Sensors;

import java.util.Date;
//import com.google.appengine.api.datastore.GeoPt;

public class DataPoint {
	private final int SIZE_OF_INPUT_ARRAY = 1*8 + 14*4;
	
	
	public final Date time;
	
//	public final GeoPt geopt;
	public final float d;
	
	public final float vx;
	public final float vy;
	public final float v;
	
	public final float ax;
	public final float ay;
	public final float az;
	public final float a;
	
	public final float jx;
	public final float jy;
	public final float jz;
	public final float j;
	
	public DataPoint(byte[] input_array) throws IllegalArgumentException {
		if (input_array.length != SIZE_OF_INPUT_ARRAY) {
			IllegalArgumentException e = new IllegalArgumentException("Input array must be exactly " + SIZE_OF_INPUT_ARRAY + " bytes");
			throw e;
		}
		this.time = new Date(getLongFromArray(input_array, 0) / 1000000);
		
//		this.geopt = new GeoPt(getFloatFromArray(input_array, 8), getFloatFromArray(input_array, 12));
		this.d  = getFloatFromArray(input_array, 16);
		
		this.vx = getFloatFromArray(input_array, 20);
		this.vy = getFloatFromArray(input_array, 24);
		this.v  = getFloatFromArray(input_array, 28);
		
		this.ax = getFloatFromArray(input_array, 32);
		this.ay = getFloatFromArray(input_array, 36);
		this.az = getFloatFromArray(input_array, 40);
		this.a  = getFloatFromArray(input_array, 44);
		
		this.jx = getFloatFromArray(input_array, 48);
		this.jy = getFloatFromArray(input_array, 52);
		this.jz = getFloatFromArray(input_array, 56);
		this.j  = getFloatFromArray(input_array, 60);
		
	}
	
	  // Given an array and a starting index, returns the long starting at that index.
	  private static long getLongFromArray(byte[] array, int start) {
	    long result = 0;
	    int shift = (8 - 1) * 8;
	   	for (int i = 0; i < 8; i++) {
	   		result += (array[i + start] & 0xFF) << shift;
	   		shift -= 8;
	   	}
	    return result;
	  }
	  
	  // Given an array and a starting index, returns the float starting at that index.
	  // First retrieves the int starting at that index before converting the bits to a float.
	  private static float getFloatFromArray(byte[] array, int start) {
	    int result = 0;
	    int shift = (4 - 1) * 8;
	   	for (int i = 0; i < 4; i++) {
	   		result += (array[i + start] & 0xFF) << shift;
	   		shift -= 8;
	   	}
	    return Float.intBitsToFloat(result);
	  }
	
	public Date getTime() {
		return time;
	}
//	public GeoPt getGeoPt() {
//		return geopt;
//	}
	public float getD() {
		return d;
	}
	public float getVx() {
		return vx;
	}
	public float getVy() {
		return vy;
	}
	public float getV() {
		return v;
	}
	public float getAx() {
		return ax;
	}
	public float getAy() {
		return ay;
	}
	public float getAz() {
		return az;
	}
	public float getA() {
		return a;
	}
	public float getJx() {
		return jx;
	}
	public float getJy() {
		return jy;
	}
	public float getJz() {
		return jz;
	}
	public float getJ() {
		return j;
	}
}
